
import 'package:flutter/widgets.dart';
import 'package:todo_app/constants/Screen.dart';

import 'CalendarScrollPhysics.dart';
import 'CalendarUtil.dart';

typedef void ChangeCalendarShow(CalendarShow calendarShow, DateTime dateTime);

typedef double GetMaxHeight();

typedef double GetMinHeight();

typedef void UpdateOffset();


class CalendarController extends ChangeNotifier {
  ChangeCalendarShow? onChangeCalendarShow;
  late PageController pageController;
  late CalendarUtil calendarUtil;
  UpdateOffset? onUpdateOffset;
  late CalendarScrollPhysics scrollPhysics;
  DateTime? currentTime;

  nextPage({
    Duration? duration,
    Curve curve = Curves.bounceInOut
  }) {

    int index = ((pageController.page ?? 0) + 1).toInt();
    double width = Screen.width * index;
    if(scrollPhysics.status.minScrollExtent > width) {
      return;
    } else if(scrollPhysics.status.maxScrollExtent < width) {
      return;
    }

    if (duration != null) {
      pageController.nextPage(
          duration: duration,
          curve: curve
      );
    } else {
      pageController.jumpToPage(index);
    }
  }

  previousPage({
    Duration? duration,
    Curve curve = Curves.bounceInOut
  }) {

    int index = ((pageController.page ?? 0) - 1).toInt();
    double width = Screen.width * index;
    if(scrollPhysics.status.minScrollExtent > width) {
      return;
    } else if(scrollPhysics.status.maxScrollExtent < width) {
      return;
    }

    if (duration != null) {
      pageController.previousPage(
          duration: duration,
          curve: curve
      );
    } else {
      pageController.jumpToPage(index);
    }
  }


  goToToday({
    Duration? duration,
    Curve curve = Curves.bounceInOut
  }) {
    var now = DateTime.now();
    goToDate(dateTime: now, duration: duration, curve: curve);
  }

  goToDate({
    required DateTime dateTime,
    Duration? duration,
    Curve curve = Curves.bounceInOut
  }) {

    currentTime = dateTime;
    int index = calendarUtil.indexOf(dateTime);

    double width = Screen.width * index;
    if(scrollPhysics.status.minScrollExtent > width) {
      return;
    } else if(scrollPhysics.status.maxScrollExtent < width) {
      return;
    }

    if (duration != null) {
      pageController.animateToPage(
          index,
          duration: duration,
          curve: curve
      );
    } else {
      pageController.jumpToPage(index);
    }
  }

  changeCalendarShow(CalendarShow calendarShow, DateTime dateTime) {
    if (onChangeCalendarShow != null) {
      onChangeCalendarShow!(calendarShow, dateTime);
    }
  }

  double getMaxHeight(double childAspectRatio,int firstDayOfWeek, DateTime currentSelectedTime) {

    double radio = CalendarUtil.aspectRatio(childAspectRatio, firstDayOfWeek, currentSelectedTime, CalendarShow.calendarShowMonth);
    return Screen.width / radio;
  }

  double getMinHeight(double childAspectRatio,int firstDayOfWeek, DateTime currentSelectedTime) {

    double radio = CalendarUtil.aspectRatio(childAspectRatio, firstDayOfWeek, currentSelectedTime, CalendarShow.calendarShowWeek);
    return Screen.width / radio;
  }

  void updateOffset(){
    if (onUpdateOffset != null) {
      onUpdateOffset!();
    }
  }


}